package com.rw.coco.expression.parser;

import com.rw.coco.expression.DiceExpression;
import com.rw.tool.util.expression.expression.InfixExpression;
import com.rw.tool.util.expression.expression.parser.InfixExpressionParser;
import com.rw.tool.util.expression.expression.parser.element.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 骰子表达式解析器
 */
public class DiceExpressionParser extends InfixExpressionParser {
    private final int defaultDice ;

    public DiceExpressionParser(int defaultDice) {
        this.defaultDice = defaultDice;
    }

    public DiceExpression parseDiceExpression(String expression) {
        InfixExpression parse = parse(expression, -1);
        return new DiceExpression(parse.getElements());
    }

    @Override
    protected List<ElementParser<?>> getElementParser() {
        List<ElementParser<?>> list = new ArrayList<>(4);

        list.add(new RfNumberElementParser());
        list.add(new RdNumberElementParser(defaultDice));
        list.add(new NumberElementParser());
        list.add(new CalculationElementParser());
        list.add(new LeftParenthesesElementParser());
        list.add(new RightParenthesesElementParser());

        return list;
    }
}
